package com.hyr.spark.streaming

import com.hyr.spark.utils.SparkUtils
import org.apache.spark.SparkConf
import org.apache.spark.examples.streaming.StreamingExamples
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
 * To run this on your local machine, you need to first run a Netcat server
 * `$ nc -lk 9999`
 * and then run the example
 * `$ bin/run-example org.apache.spark.examples.streaming.JavaNetworkWordCount localhost 9999`
 */
object NetworkWordCount {
  def main(args: Array[String]): Unit = {

    //1. 配置文件
    val sparkConf: SparkConf = new SparkConf().setAppName("wc").setMaster("local")
    //2.构建ssc
    val ssc: StreamingContext = new StreamingContext(sparkConf, Seconds(5))
    //3.输入数据
    val stream: ReceiverInputDStream[String] = ssc.socketTextStream("gpu116", 9999)
    //4.数据处理
    val resutlStream: DStream[(String, Int)] = stream.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)
    //5.结果保存或者打印
    resutlStream.print()
    //6.启动Spark流计算
    ssc.start()
  }

}
